package com.hotel.controller;

import com.alibaba.fastjson.JSONObject;
import com.hotel.component.BaseController;
import com.hotel.entity.Option;
import com.hotel.entity.Role;
import com.hotel.utils.JdbcUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Str
 * @Date 2021/10/28 21:21
 * @Description :角色管理
 */
@WebServlet("/role/*")
public class RoleController extends BaseController {

    public String getRoleData(HttpServletRequest req, HttpServletResponse resp) {
        String sqlQuery = "select * from role";
        List<Role> roles = null;
        JSONObject jsObj = null;
        try {
            roles = JdbcUtils.queryList(sqlQuery, Role.class);
            List<Option> options = new ArrayList();
            // 将楼层和id填充进Option列表
            for (Role role : roles) {
                Option op = new Option(role.getRoleName(), role.getId());
                options.add(op);
            }
            jsObj = new JSONObject();
            jsObj.put("result", "success");
            jsObj.put("options", options);
        } catch (SQLException e) {
            e.printStackTrace();
            jsObj.put("result", "failed");
        }
        return "data:" + jsObj;
    }

}

